/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pagrafo.janela;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import pagrafo.controle.Controle;
import pagrafo.genetico.Roteamento;

/**
 *
 * @author Fabricio
 */
public class JanelaDeDesenho extends javax.swing.JFrame {

    private Controle c;

    /**
     * Creates new form JanelaDeDesenho
     */
    public JanelaDeDesenho(Controle c) {
        this.c = c;
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        botaoAbrir = new javax.swing.JButton();
        botaoExecutar = new javax.swing.JButton();
        campoCaminhoes = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        campoInteracoes = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        campoPopulacao = new javax.swing.JTextField();
        campoCapacidade = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        campoDemanda = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        area = new pagrafo.janela.AreaDeDesenho(c,true);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        botaoAbrir.setText("Abrir Arquivo");
        botaoAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoAbrirActionPerformed(evt);
            }
        });

        botaoExecutar.setText("Executar");
        botaoExecutar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoExecutarActionPerformed(evt);
            }
        });

        campoCaminhoes.setText("3");

        jLabel1.setText("Número de Caminhões");

        jLabel2.setText("Número de Interações do AG");

        campoInteracoes.setText("200");

        jLabel3.setText("População Inicial");

        campoPopulacao.setText("50");

        campoCapacidade.setText("500");

        jLabel4.setText("Capacidade");

        campoDemanda.setText("20");

        jLabel5.setText("Demanda padrão");

        area.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                areaMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout areaLayout = new org.jdesktop.layout.GroupLayout(area);
        area.setLayout(areaLayout);
        areaLayout.setHorizontalGroup(
            areaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 420, Short.MAX_VALUE)
        );
        areaLayout.setVerticalGroup(
            areaLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 422, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(campoDemanda, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5)
                    .add(campoCaminhoes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2)
                    .add(campoInteracoes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(campoPopulacao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(campoCapacidade, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(botaoExecutar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(botaoAbrir, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 180, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(area, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(botaoAbrir)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(botaoExecutar)
                        .add(24, 24, 24)
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(campoCaminhoes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(campoCapacidade, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(campoPopulacao, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(campoInteracoes, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 132, Short.MAX_VALUE)
                        .add(jLabel5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(campoDemanda, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(4, 4, 4)
                        .add(area, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoAbrirActionPerformed
        String caminho = JOptionPane.showInputDialog(null, "Digite o caminho do arquivo", "Caminho do arquivo", JOptionPane.INFORMATION_MESSAGE);
        if (caminho.equals("")) {
            caminho = "grafo.vrp";
        }
        try {
            c.lerArquivo(caminho);
            
            this.area.setGrafo(c.getGrafo());
            this.area.setModoDesenho(false);
            this.area.setZoom(6);

            this.validate();
            this.repaint();
        } catch (FileNotFoundException ex) {
            JOptionPane.showMessageDialog(null, "O arquivo não pode ser encontrado");
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "Erro de entrada, aquivo fora do formato");
        }
    }//GEN-LAST:event_botaoAbrirActionPerformed

    private void botaoExecutarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoExecutarActionPerformed
        c.executar(this.getCaminhoes(), this.getPopulacao(), this.getGeracoes());
     
        int v[] = c.getRoteamento();
       
        for (int i = 0; i < v.length; i++) {
            System.out.println(v[i]);
        }
        System.out.println("fim");
    }//GEN-LAST:event_botaoExecutarActionPerformed

    private void areaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_areaMouseClicked
        if (evt.BUTTON1 == evt.getButton()) {
            this.area.adicionarPonto(evt.getX(), evt.getY(), Double.valueOf(campoDemanda.getText()));
            //System.out.println(evt.getX() + " " + evt.getY());
        }
        this.repaint();
    }//GEN-LAST:event_areaMouseClicked

    public void setCaminhoes(int k) {
        campoCaminhoes.setText(String.valueOf(k));
    }

    public void setCapacidade(double c) {
        campoCapacidade.setText(String.valueOf(c));
    }

    public int getCaminhoes() {
        try {
            return Integer.parseInt(campoCaminhoes.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "O número de caminhões não é válido, foi atribuido 1");
            return 1;
        }
    }

    public int getGeracoes() {
        try {
            return Integer.parseInt(campoInteracoes.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "O número de gerações não é válido, foi atribuido 100");
            return 100;
        }
    }

    public int getPopulacao() {
        try {
            int p = Integer.parseInt(campoPopulacao.getText());
            p = (int) p / 2;
            p = p * 2;
            return p;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "O número de individuos na população não é válido, foi atribuido 100");
            return 100;
        }
    }

    public double getCapacidade() {
        try {
            return Double.parseDouble(campoPopulacao.getText());
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "A capacidade de 1 caminhão não é válido, foi atribuido 1000");
            return 1000;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private pagrafo.janela.AreaDeDesenho area;
    private javax.swing.JButton botaoAbrir;
    private javax.swing.JButton botaoExecutar;
    private javax.swing.JTextField campoCaminhoes;
    private javax.swing.JTextField campoCapacidade;
    private javax.swing.JTextField campoDemanda;
    private javax.swing.JTextField campoInteracoes;
    private javax.swing.JTextField campoPopulacao;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    // End of variables declaration//GEN-END:variables

}
